package com.straw.jdk.algorithm.structure.stack;

import com.sun.org.apache.regexp.internal.RE;

import java.util.Arrays;

/**
 * @author jianshan@byai.com
 * @date 2020/12/4 5:52 PM
 * describe:
 */
public class ArrayStack {


    private Object[] stack = new Object[10];

    private int size = 0;


    public Object peek() {
        return size == 0 ? null : stack[size - 1];
    }


    public Object pop() {
        Object head = peek();
        size = size-- <= 0 ? 0 : size;
        return head;
    }


    public boolean push(Object obj) {
        if (!expandCapacity(size + 1)) {
            throw new RuntimeException("Capacity Limit Exceeded");
        }
        stack[size] = obj;
        size++;
        return true;
    }


    private boolean expandCapacity(int i) {
        if (i >= Integer.MAX_VALUE) {
            return false;
        }
        size = size << 1;
        stack = Arrays.copyOf(stack, size);
        return true;
    }


}
